var index = 0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageindex: 0,
    questionLib: "",
    questioncontent: "",
    questionImageUrl: "",
    item1: '',
    item2: '',
    item3: '',
    item4: '',
    explains: '',
    answer: '',
    answer_status: 'none'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.request({
      url: 'http://v.juhe.cn/jztk/query?subject=' + options.subject + '&model=' + options.model + '&testType=rand&=&key=53af3ee326412e2a96d1dadace4efdfc',
      success: (res) => {
        console.log(res)
        this.setData({
          pageindex: index,
          questionLib: res.data.result,
          questioncontent: res.data.result[index].question,
          questionImageUrl: res.data.result[index].url,
          item1: res.data.result[index].item1,
          item2: res.data.result[index].item2,
          item3: res.data.result[index].item3,
          item4: res.data.result[index].item4,
          explains: res.data.result[index].explains,
          answer: res.data.result[index].answer,
        })
        console.log(this.data.questionLib[index].question)
      }, fail: (res) => {
        wx.showToast({
          title: '获取题库失败',
          icon: 'none'
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({

    })
  },
  onNext: function () {
    index++;
    this.setData({
      pageindex: index,
      questioncontent: this.data.questionLib[index].question,
      questionImageUrl: this.data.questionLib[index].url,
      item1: this.data.questionLib[index].item1,
      item2: this.data.questionLib[index].item2,
      item3: this.data.questionLib[index].item3,
      item4: this.data.questionLib[index].item4,
      explains: this.data.questionLib[index].explains,
      answer: this.data.questionLib[index].answer,
      answer_status: 'none'
    })
  },
  onLast: function () {
    index--;
    this.setData({
      pageindex: index,
      questioncontent: this.data.questionLib[index].question,
      questionImageUrl: this.data.questionLib[index].url,
      item1: this.data.questionLib[index].item1,
      item2: this.data.questionLib[index].item2,
      item3: this.data.questionLib[index].item3,
      item4: this.data.questionLib[index].item4,
      explains: this.data.questionLib[index].explains,
      answer: this.data.questionLib[index].answer,
      answer_status: 'none'
    })
  },
  examChangeBox: function () {
    switch (this.data.answer) {
      case '1':
        this.setData({ answerTy: 'A' })
        break;
      case '2':
        this.setData({ answerTy: 'B' })
        break;
      case '3':
        this.setData({ answerTy: 'C' })
        break;
      case '4':
        this.setData({ answerTy: 'D' })
        break;
    }
    this.setData({
      answer_status: 'block'
    })

  },
  onA: function () {
    this.examChangeBox()
    if (this.data.answer == 1) {
      wx.showToast({
        title: '回答正确',
        icon: 'success'
      })
    } else {
      wx.showToast({
        title: '回答错误',
        icon: 'none'
      })
    }
  },
  onB: function () {
    if (this.data.answer == 2) {
      wx.showToast({
        title: '回答正确',
        icon: 'success'
      })
    } else {
      wx.showToast({
        title: '回答错误',
        icon: 'none'
      })
    }
    this.examChangeBox()
  },
  onC: function () {
    this.examChangeBox()
    if (this.data.answer == 3) {
      wx.showToast({
        title: '回答正确',
        icon: 'success'
      })
    } else {
      wx.showToast({
        title: '回答错误',
        icon: 'none'
      })
    }
  },
  onD: function () {
    this.examChangeBox()
    if (this.data.answer == 4) {
      wx.showToast({
        title: '回答正确',
        icon: 'success'
      })
    } else {
      wx.showToast({
        title: '回答错误',
        icon: 'none'
      })
    }
  },
})